<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="sku" prop="sku">
        <el-input
          v-model="queryParams.sku"
          placeholder="请输入sku"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-input
          v-model="queryParams.channel"
          placeholder="请输入渠道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          :icon="!isOpen ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
          size="mini"
          @click="shouqi"
          >展开/收起</el-button
        >
      </el-form-item>

      <div v-if="isOpen">
        <el-form-item label="是否在销" prop="isOnSale">
          <el-input
            v-model="queryParams.isOnSale"
            placeholder="请输入是否在销"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="库存" prop="inventoryQuantity">
          <el-input
            v-model="queryParams.inventoryQuantity"
            placeholder="请输入库存"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="总库存" prop="totalInventory">
          <el-input
            v-model="queryParams.totalInventory"
            placeholder="请输入总库存"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售量" prop="salesQuantitySaleDays">
          <el-input
            v-model="queryParams.salesQuantitySaleDays"
            placeholder="请输入销售量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售天数" prop="saleDays">
          <el-input
            v-model="queryParams.saleDays"
            placeholder="请输入销售天数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="7天销售量" prop="sevenSaleNum">
          <el-input
            v-model="queryParams.sevenSaleNum"
            placeholder="请输入7天销售量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="15天销售量" prop="fifteenSaleNum">
          <el-input
            v-model="queryParams.fifteenSaleNum"
            placeholder="请输入15天销售量"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="上架时间" prop="newTime">
          <el-date-picker
            clearable
            v-model="queryParams.newTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上架时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否新品" prop="isNewProduct">
          <el-input
            v-model="queryParams.isNewProduct"
            placeholder="请输入是否新品"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="季节" prop="productSeason">
          <el-input
            v-model="queryParams.productSeason"
            placeholder="请输入季节"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </div>
      <!-- <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sale:BiSkuInfoDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sale:BiSkuInfoDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sale:BiSkuInfoDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sale:BiSkuInfoDwd:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" @click="handlesynData">同步</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" @click="handleRuleTagger">打标</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" @click="handleGo('/biTool/biruletag')"
          >配置规则</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button size="mini" @click="handleGo('/conf/BiCalculatorVariables')"
          >配置变量</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiSkuInfoDwdList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="sku" align="center" prop="sku" />
      <el-table-column label="渠道" align="center" prop="channel" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="是否在销" align="center" prop="isOnSale" />
      <el-table-column label="库存类型" align="center" prop="inventoryType" />
      <el-table-column label="是否无动销" align="center" prop="isNoneSale" />
      <el-table-column label="库存" align="center" prop="inventoryQuantity" />
      <el-table-column label="总库存" align="center" prop="totalInventory" />
      <el-table-column
        label="销售量"
        align="center"
        prop="salesQuantitySaleDays"
      />
      <el-table-column label="销售天数" align="center" prop="saleDays" />
      <el-table-column label="7天销售量" align="center" prop="sevenSaleNum" />
      <el-table-column
        label="15天销售量"
        align="center"
        prop="fifteenSaleNum"
      />
      <el-table-column
        label="上架时间"
        align="center"
        prop="newTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.newTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否新品" align="center" prop="isNewProduct" />
      <el-table-column label="季节" align="center" prop="productSeason" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sale:BiSkuInfoDwd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sale:BiSkuInfoDwd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改sku标签信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="sku" prop="sku">
          <el-input v-model="form.sku" placeholder="请输入sku" />
        </el-form-item>
        <el-form-item label="渠道" prop="channel">
          <el-input v-model="form.channel" placeholder="请输入渠道" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="是否在销" prop="isOnSale">
          <el-input v-model="form.isOnSale" placeholder="请输入是否在销" />
        </el-form-item>
        <el-form-item label="库存" prop="inventoryQuantity">
          <el-input v-model="form.inventoryQuantity" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="总库存" prop="totalInventory">
          <el-input v-model="form.totalInventory" placeholder="请输入总库存" />
        </el-form-item>
        <el-form-item label="销售量" prop="salesQuantitySaleDays">
          <el-input
            v-model="form.salesQuantitySaleDays"
            placeholder="请输入销售量"
          />
        </el-form-item>
        <el-form-item label="销售天数" prop="saleDays">
          <el-input v-model="form.saleDays" placeholder="请输入销售天数" />
        </el-form-item>
        <el-form-item label="7天销售量" prop="sevenSaleNum">
          <el-input v-model="form.sevenSaleNum" placeholder="请输入7天销售量" />
        </el-form-item>
        <el-form-item label="15天销售量" prop="fifteenSaleNum">
          <el-input
            v-model="form.fifteenSaleNum"
            placeholder="请输入15天销售量"
          />
        </el-form-item>
        <el-form-item label="上架时间" prop="newTime">
          <el-date-picker
            clearable
            v-model="form.newTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择上架时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否新品" prop="isNewProduct">
          <el-input v-model="form.isNewProduct" placeholder="请输入是否新品" />
        </el-form-item>
        <el-form-item label="季节" prop="productSeason">
          <el-input v-model="form.productSeason" placeholder="请输入季节" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiRuleSpecifications,
  getBiRuleSpecifications,
  delBiRuleSpecifications,
  addBiRuleSpecifications,
  updateBiRuleSpecifications,
  RuleTagger,
  getTablefieldsWithComments,
} from "@/api/conf/BiRuleSpecifications";
import {
  listBiSkuInfoDwd,
  getBiSkuInfoDwd,
  delBiSkuInfoDwd,
  addBiSkuInfoDwd,
  updateBiSkuInfoDwd,
  synData,
} from "@/api/sale/BiSkuInfoDwd";

export default {
  name: "BiSkuInfoDwd",
  data() {
    return {
      isOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // sku标签信息表格数据
      BiSkuInfoDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sku: null,
        channel: null,
        brand: null,
        isOnSale: null,
        inventoryType: null,
        saleType: null,
        inventoryQuantity: null,
        totalInventory: null,
        salesQuantitySaleDays: null,
        saleDays: null,
        sevenSaleNum: null,
        fifteenSaleNum: null,
        newTime: null,
        isNewProduct: null,
        productSeason: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sku: [{ required: true, message: "sku不能为空", trigger: "blur" }],
        channel: [{ required: true, message: "渠道不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleGo(path) {
      this.$router.push({
        path: path,
        query: { tableName: "bi_sku_info_dwd" },
      });
    },
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在同步数据，请稍候！");
    },
    handlesynData() {
      this.openLoading();
      synData().then((response) => {
        this.$modal.closeLoading();
        if (response.code == 200) {
          this.$modal.msgSuccess("同步成功");
          this.getList();
        }
      });
    },
    handleRuleTagger() {
      this.openLoading();
      RuleTagger({ tableName: "bi_sku_info_dwd" }).then((response) => {
        this.$modal.closeLoading();
        if (response.code == 200) {
          this.$modal.msgSuccess("打标成功");
          this.getList();
        }
      });
    },
    //搜索栏展示与收起
    shouqi() {
      // console.log(1);
      this.isOpen = !this.isOpen;
    },
    /** 查询sku标签信息列表 */
    getList() {
      this.loading = true;
      listBiSkuInfoDwd(this.queryParams).then((response) => {
        this.BiSkuInfoDwdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        sku: null,
        channel: null,
        brand: null,
        isOnSale: null,
        inventoryType: null,
        saleType: null,
        inventoryQuantity: null,
        totalInventory: null,
        salesQuantitySaleDays: null,
        saleDays: null,
        sevenSaleNum: null,
        fifteenSaleNum: null,
        newTime: null,
        isNewProduct: null,
        productSeason: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加sku标签信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiSkuInfoDwd(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改sku标签信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiSkuInfoDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiSkuInfoDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除sku标签信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiSkuInfoDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sale/BiSkuInfoDwd/export",
        {
          ...this.queryParams,
        },
        `BiSkuInfoDwd_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
